<?php
 require "../document/class_corpus.php";
 require "../document/class_document.php";
 require "../document/class_document_xml.php";

 require "../files/tool_files_dirs.php";
 require "../misc/class_tag.php";

 require "../tree/class_tree.php";
 require "../tree/class_tree_xhtml.php";
 require "../tree/class_structure.php";
 require "../tree/tool_tree.php";

 require "../array/tool_array.php";

 require "../string/tool_string.php";
 require "../string/class_segment.php";

 require "../pdo/class_sql_document.php";

function is_union($o,$l,$o1,$l1){
  $e  = $o + $l - 1;
  $e1 = $o1 + $l1 - 1;
  if(($o1 < $o AND $e1 >= $o) OR ($o1 > $o AND $o1 <= $e) OR ($e1 > $e AND $o1 <= $e))
    return TRUE;
  else
    return FALSE;
}

function is_inside($o,$l,$o1,$l1){
  $e  = $o + $l - 1;
  $e1 = $o1 + $l1 - 1;
  if($o1 >= $o AND $e1 <= $e)
    return TRUE;
  else
    return FALSE;
}

function print_substr($array_substr){
  foreach($array_substr as $c){
    echo "    [". $c['graphie'] ."] ". $c['offset'] ." ". $c['len'] ."\n";
  }
}

function print_inside($_g,$_o,$_l,$insides){
  $res = "[$_g]\n";
  foreach($insides as $in){
    $res.="[";
    for($i=0;$i<($in['offset']-$_o);++$i){
      $res.="_";
    }
    $res.=$in['graphie'];
    for($i=0;$i<($_o+$_l - $in['offset'] - $in['len']);++$i){
      $res.="_";
    }
    $res .= "]\n";
  }
  print $res;
}

function print_outside($_g,$_o,$_l,$outsides){
  $res = "[$_g]\n";
  $e = $_o + $_l - 1;
  foreach($outsides as $out){
    $o1 = $out['offset'];
    $l1 = $out['len'];
    $e1 = $o1 + $l1 - 1;
    $min = min($_o,$o1);
    $max = max($_e,$e1);
    $nb_inf = abs($o1 - $min);
    $nb_sup = abs($max - $e1);
//    print $out['graphie']."::".$nb_inf." ".$nb_sup."\n";
    $res .= "[";
    for($i=0;$i<$nb_inf;++$i) $res .= "~";
    $res .= $out['graphie'];
    for($i=0;$i<$nb_sup;++$i) $res .= "~";
    $res .= "]\n";
  }
  print $res;
}

$exp=new sql_experience("align_ecr_selected");
$array_obj_sql=$exp->reader_experience();
$array_data=array();

$data_lg=$array_obj_sql['sql_document']->reader_all_lg();

$corpus_multidoc = array();

//$data_lg = array( array('lg'=>'es'),array('lg'=>'fr') );
$id_chunk = 1;

$array_factory = array();
foreach($data_lg as $lg){
  $array_factory[$lg['lg']] = new factory_mot();
  $r = $array_obj_sql['sql_rstr']->reader_all2relative_id_doc($lg["lg"]);
  $k = key($r);
  foreach($r[$k] as $tid=>$id){
    $corpus_multidoc[$id][$k] = $tid;
  }
}

$doc2bloc = array();
$id_bloc2text = array();

//$array_word = $array_obj_sql['sql_chunk']->reader_id_bloc2info_chunk(2267);
//print_r($array_word);
//die();

foreach($corpus_multidoc as $id_multidoc=>$multidoc){
  foreach($multidoc as $lg=>$id_doc){
  $r = $array_obj_sql['sql_bloc']->reader_id_document2array($id_doc);
    foreach($r as $info_bloc){
      $id_bloc = $info_bloc['id_bloc'];
      $id_bloc2text[$id_bloc] = $info_bloc['contenu_text'];
      $array_word = $array_obj_sql['sql_chunk']->reader_id_bloc2info_chunk($id_bloc);
      $bloc2chunk[$lg][$id_doc][$id_bloc] = $array_word;
    }
  }
}

$id_multidoc = 1;
$multidoc = $corpus_multidoc[$id_multidoc];
$lg_src = 'fr';
$lg_tgt = 'es';
$id_doc_src = $multidoc[$lg_src];
$id_doc_tgt = $multidoc[$lg_tgt];

$bc_src = $bloc2chunk[$lg_src][$id_doc_src];
$bc_tgt = $bloc2chunk[$lg_tgt][$id_doc_tgt];

function bc2ac($v,&$txt){
  $ac = array();
  foreach($v as $id_chunk => $array_mot){
    $am = array();
    foreach($array_mot as $i){
      $mot = new mot($txt, $i[1], $i[2], array("type"=>$i[4]));
      $am[] = $mot;
    }
    $chunk = new chunk($am);
    $ac[] = $chunk;
  }
  return $ac;
}

function chunk2rstr($ac,$id_bloc,&$array_obj_sql){
  $res = array();
  foreach($ac as $id=>$c_src){
    $o_src = $c_src->__get_offset();
    $l_src = $c_src->__get_len();
    $candidats = $array_obj_sql['sql_rstr']->reader_seg2seg_inside($id_bloc,$o_src,$l_src);
    $inside = $union = $outside = array();
    foreach($candidats as $cand){
      (is_inside($o_src,$l_src,$c['offset'],$c['len'])) ? $inside[]=$cand : $outside[]=$cand;
    }
    $res[$id] = array('outside' => $outside, 'inside' => $inside);
  }
  return $res;
}


$time_start = microtime(true);
$cpt = 0;

//$bc = before chunk
//$ac = array chunk


foreach($data_lg as $lg_info){
    $lg = $lg_info[0];
    $nb_chunk = 0;
    $structure_chunk = array();
    foreach($bloc2chunk[$lg] as $bc_src){
        while(list($k1,$v1) = each($bc_src)){
            $ac_src = bc2ac($v1,$id_bloc2text[$k1]);
            foreach($ac_src as $chunk){
                $csign = $chunk->__get_signature();
                //      if($csign == 'vvvvvpp') echo $chunk."\n";
                $structure_chunk[$csign]++;
                $nb_chunk++;
            }
        }
    }
    arsort($structure_chunk);
    echo "$lg\n";
    print_r($structure_chunk);
    echo $nb_chunk."\n";
}
die();

while(list($k1,$v1) = each($bc_src) and list($k2,$v2) = each($bc_tgt)){
  $ac_src = bc2ac($v1,$id_bloc2text[$k1]);
  $ac_tgt = bc2ac($v2,$id_bloc2text[$k2]);
  if($cpt == 1){
    print_r($ac_tgt);
    foreach($ac_tgt as $c){
      print_r($c);
      print $id_bloc2text[$k2];
      print "\n";
      print $c;
      print "\n";
    }
  }
//  $chunk = reset($ac_src);
//  $g = $chunk->__get_str();
  $rstr_src = chunk2rstr($ac_src,$k1,$array_obj_sql);
  $rstr_tgt = chunk2rstr($ac_tgt,$k2,$array_obj_sql);

/*
  foreach($rstr_src as $id_chunk=>$info){
      $chunk = $ac_src[$id_chunk];
      $g = "$chunk";
      $o = $chunk->__get_offset();
      $l = $chunk->__get_len();
      echo "inside\n";
      print_inside($g,$o,$l,$info['inside']);
      echo "outside\n";
      print_outside($g,$o,$l,$info['outside']);
      die();
  }
*/
  if($cpt == 1) die();
  ++$cpt;
}
$time_end = microtime(true);
$time=$time_end-$time_start;
echo "time=$time\n";
  die();

?>
